package com.ruoyi.system.service.impl;

import java.util.List;

import com.ruoyi.common.enums.TenantStatus;
import com.ruoyi.common.enums.UnitStatus;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.DoubleUtil;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.domain.*;
import com.ruoyi.system.mapper.SysHigherMapper;
import com.ruoyi.system.mapper.SysUnitMapper;
import com.ruoyi.system.mapper.SysUnitTenantMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.SysTenantMapper;
import com.ruoyi.system.service.ISysTenantService;
import com.ruoyi.common.core.text.Convert;
import org.springframework.transaction.annotation.Transactional;

/**
 * 合同Service业务层处理
 * 
 * @author ruoyi
 * @date 2020-06-28
 */
@Service
public class SysTenantServiceImpl implements ISysTenantService
{
    @Autowired
    private SysTenantMapper sysTenantMapper;

    @Autowired
    private SysUnitTenantMapper sysUnitTenantMapper;

    @Autowired
    private SysUnitMapper sysUnitMapper;

    @Autowired
    private SysHigherMapper sysHigherMapper;

    /**
     * 查询合同
     *
     * @param tenantId 合同ID
     * @return 合同
     */
    @Override
    public SysTenant selectSysTenantById(Long tenantId)
    {
        return sysTenantMapper.selectSysTenantById(tenantId);
    }

    /**
     * 待收租定时任务（个人/企业）
     *
     * @param sysTenant
     */
    @Override
    public List<SysTenant> selectSysBacklogTaskList(SysTenant sysTenant) {
        return sysTenantMapper.selectSysBacklogTaskList(sysTenant);
    }

    /**
     * 查询合同根据unitIdStr
     *
     * @param unitIdStr 单元
     * @return 合同
     */
    @Override
    public List<SysTenant> selectSysTenantByUnitId(String unitIdStr) {
        if(StringUtils.isNotBlank(unitIdStr)){
            return sysTenantMapper.selectSysTenantByUnitId(unitIdStr);
        }
        return null;
    }

    /**
     * 查询面积总和(个人或者企业)
     *
     *  @param
     *  @return SysTenant
     */
    @Override
    public SysTenant selectSysTenantOtherArea() {
        return sysTenantMapper.selectSysTenantOtherArea();
    }

    /**
     * 查询面积总和(宿舍)
     *
     *   @param
     *   @return SysTenant
     */
    @Override
    public SysTenant selectSysTenantAccommodationArea() {
        return sysTenantMapper.selectSysTenantAccommodationArea();
    }

    /**
     * 查询合同根据buildingId
     *
     * @param buildingId 楼栋ID
     * @return 合同
     */
    @Override
    public List<SysTenant> selectSysTenantByBuildingId(Long buildingId) {
        if(buildingId!=null){
            return sysTenantMapper.selectSysTenantByBuildingId(buildingId);
        }
        return null;
    }

    /**
     * 查询合同列表
     *
     * @param sysTenant 合同
     * @return 合同
     */
    @Override
    public List<SysTenant> selectSysTenantList(SysTenant sysTenant)
    {
        return sysTenantMapper.selectSysTenantList(sysTenant);
    }

    /**
     * 查询合同导出合同列表
     *
     * @param sysTenant 合同
     * @return 合同集合
     */
    @Override
    public List<SysTenantExcel> selectSysTenantExcelList(SysTenant sysTenant) {
        return sysTenantMapper.selectSysTenantExcelList(sysTenant);
    }

    @Override
    public List<SysTenantExcel> selectSysTenantLeaveExcelList(SysTenant sysTenant) {
        return sysTenantMapper.selectSysTenantLeaveExcelList(sysTenant);
    }

    /**
     * 查询合同导出合计
     *
     * @param sysTenant 合同ID
     * @return 合同
     */
    @Override
    public SysTenantExcel selectSysTenantExcelSum(SysTenant sysTenant) {
        return sysTenantMapper.selectSysTenantExcelSum(sysTenant);
    }

    /**
     * 查询合同涨租明细列表
     *
     * @param sysTenant 合同
     * @return 合同集合
     */
    @Override
    public List<SysTenantHigher> selectSysTenantHigherExcelList(SysTenant sysTenant) {
        return sysTenantMapper.selectSysTenantHigherExcelList(sysTenant);
    }

    /**
     * 查询合同涨租列表
     *
     * @param sysTenant 合同
     * @return 合同集合
     */
    @Override
    public List<SysTenant> selectSysTenantHigherList(SysTenant sysTenant) {
        return sysTenantMapper.selectSysTenantHigherList(sysTenant);
    }




    /**
     * 查询合同
     *
     * @param tenantId 合同ID
     * @return 合同
     */
    @Override
    public SysTenant selectSysTenantBacklogByIds(String tenantId) {
        return sysTenantMapper.selectSysTenantBacklogByIds(Convert.toStrArray(tenantId));
    }

    /**
     * 新增合同
     *
     * @param sysTenant 合同
     * @return 结果
     */
    @Override
    @Transactional
    public int insertSysTenant(SysTenant sysTenant)
    {
        //新增合同
        sysTenant.setCreateTime(DateUtils.getNowDate());
        int rows = sysTenantMapper.insertSysTenant(sysTenant);

        for (String unitId : sysTenant.getUnitIdStr().split(",")) {
            //新增合同单元关联
            SysUnitTenant sysUnitTenant = new SysUnitTenant(Long.parseLong(unitId),sysTenant.getTenantId());
            sysUnitTenantMapper.insertSysUnitTenant(sysUnitTenant);
            //修改单元状态
            SysUnit sysUnit = new SysUnit();
            sysUnit.setUnitId(Long.parseLong(unitId));
            sysUnit.setUnitStatus(UnitStatus.RENTING.getCode());
            sysUnitMapper.updateSysUnit(sysUnit);
        }
        //新增合同涨租表首次月租日志
        SysHigher sysHigher = new SysHigher();
        sysHigher.setTenantId(sysTenant.getTenantId());
        sysHigher.setHigherRentFee(Double.parseDouble(sysTenant.getMonthlyRentFee()));
        sysHigher.setDelFlag("1");//1 - 代表标记为第一次租金记录
        sysHigherMapper.insertSysHigher(sysHigher);

        return rows;
    }

    /**
     * 合同续租
     *
     * @param sysTenant 合同
     * @return 结果
     */
    @Override
    @Transactional
    public int insertSysTenantContinue(SysTenant sysTenant) {
        //离租
        SysTenant leaveTenant = new SysTenant();
        leaveTenant.setTenantId(sysTenant.getTenantId());
        leaveTenant.setTenantStatus(TenantStatus.LEAVE.getCode());
        leaveTenant.setLeaveTime(DateUtils.getNowDate());
        leaveTenant.setRemark(sysTenant.getRemark());
        leaveSysTenant(leaveTenant);

        //新增合同
        SysTenant sysTenantSource = sysTenantMapper.selectSysTenantById(sysTenant.getTenantId());
        SysTenant sysTenantNew = new SysTenant();
        BeanUtils.copyProperties(sysTenantSource,sysTenantNew);
        sysTenantNew.setStartTime(sysTenant.getStartTime());
        sysTenantNew.setEndTime(sysTenant.getEndTime());
        sysTenantNew.setRisePriceFirstTime(sysTenant.getRisePriceFirstTime());
        sysTenantNew.setRisePriceRatio(sysTenant.getRisePriceRatio());
        sysTenantNew.setMonthlyRentFee(sysTenant.getMonthlyRentFee());
        sysTenantNew.setTenantStatus(TenantStatus.LIVEING.getCode());
        sysTenantNew.setRemark("");
        int rows = insertSysTenant(sysTenantNew);

        return rows;
    }

    /**
     * 修改合同
     *
     * @param sysTenant 合同
     * @return 结果
     */
    @Override
    @Transactional
    public int updateSysTenant(SysTenant sysTenant)
    {
        sysTenant.setUpdateTime(DateUtils.getNowDate());
        return sysTenantMapper.updateSysTenant(sysTenant);
    }

    /**
     * 定时任务接口-修改涨租状态
     *
     * @param sysTenant 合同
     * @return 结果
     */
    @Override
    public int updateSysTenantRisePriceStatus(SysTenant sysTenant) {
        sysTenant.setUpdateTime(DateUtils.getNowDate());
        return sysTenantMapper.updateSysTenantRisePriceStatus(sysTenant);
    }

    /**
     * 合同离租
     *
     * @param sysTenant 合同
     * @return 结果
     */
    @Override
    @Transactional
    public int leaveSysTenant(SysTenant sysTenant) {
        //修改合同状态
        sysTenant.setUpdateTime(DateUtils.getNowDate());
        int rows = sysTenantMapper.updateSysTenant(sysTenant);
        //修改单元状态
        SysTenant sysTenantTemp = sysTenantMapper.selectSysTenantById(sysTenant.getTenantId());
        for (String unitId : sysTenantTemp.getUnitIdStr().split(",")) {
            SysUnit sysUnit = new SysUnit();
            sysUnit.setUnitId(Long.parseLong(unitId));
            if(sysTenant.getTenantStatus().equals(TenantStatus.LEAVE.getCode())){
                sysUnit.setUnitStatus(UnitStatus.FREE.getCode());
            }else {
                sysUnit.setUnitStatus(UnitStatus.WILL.getCode());
            }
            sysUnitMapper.updateSysUnit(sysUnit);
        }
        //删除合同单元关联
        sysUnitTenantMapper.deleteSysUnitTenantByIds(sysTenantTemp.getUnitIdStr().split(","));
        return rows;
    }

    /**
     * 删除合同对象
     *
     * @param ids 需要删除的数据ID
     * @return 结果
     */
    @Override
    @Transactional
    public int deleteSysTenantByIds(String ids)
    {
        //是否离租
        List<SysTenant> list = sysTenantMapper.selectSysTenantByIds(Convert.toStrArray(ids));
        for (SysTenant sysTenant : list) {
            if(!sysTenant.getTenantStatus().equals(TenantStatus.LEAVE.getCode())){
                return 0;
            }
        }
        //删除合同
        int rows = sysTenantMapper.deleteSysTenantByIds(Convert.toStrArray(ids));
        return rows;
    }

    /**
     * 删除合同信息
     * 
     * @param tenantId 合同ID
     * @return 结果
     */
    @Override
    @Transactional
    public int deleteSysTenantById(Long tenantId)
    {
        SysTenant sysTenant = sysTenantMapper.selectSysTenantById(tenantId);
        if(!sysTenant.getTenantStatus().equals(TenantStatus.LEAVE.getCode())){
            return 0;
        }
        return sysTenantMapper.deleteSysTenantById(tenantId);
    }

    @Override
    public SysTenant exchange(SysTenant tenant) {
        if(StringUtils.isNotBlank(tenant.getMonthlyRentFee())){ tenant.setMonthlyRentFee(String.valueOf(DoubleUtil.round(Double.parseDouble(tenant.getMonthlyRentFee()),2))); }
        if(StringUtils.isNotBlank(tenant.getPropertyFee())){tenant.setPropertyFee(String.valueOf(DoubleUtil.round(Double.parseDouble(tenant.getPropertyFee()),2)));}
        if(StringUtils.isNotBlank(tenant.getPledgeFee())){tenant.setPledgeFee(String.valueOf(DoubleUtil.round(Double.parseDouble(tenant.getPledgeFee()),2)));}
        if(StringUtils.isNotBlank(tenant.getDecorationPledgeFee())){tenant.setDecorationPledgeFee(String.valueOf(DoubleUtil.round(Double.parseDouble(tenant.getDecorationPledgeFee()),2)));}
        if(StringUtils.isNotBlank(tenant.getScavengingFee())){tenant.setScavengingFee(String.valueOf(DoubleUtil.round(Double.parseDouble(tenant.getScavengingFee()),2)));}
        if(StringUtils.isNotBlank(tenant.getUtilityFee())){tenant.setUtilityFee(String.valueOf(DoubleUtil.round(Double.parseDouble(tenant.getUtilityFee()),2)));}
        return tenant;
    }

    @Override
    public SysTenantExcel exchangeTwo(SysTenantExcel tenant) {
        if(StringUtils.isNotBlank(tenant.getMonthlyRentFee())){ tenant.setMonthlyRentFee(String.valueOf(DoubleUtil.round(Double.parseDouble(tenant.getMonthlyRentFee()),2))); }
        if(StringUtils.isNotBlank(tenant.getPropertyFee())){tenant.setPropertyFee(String.valueOf(DoubleUtil.round(Double.parseDouble(tenant.getPropertyFee()),2)));}
        if(StringUtils.isNotBlank(tenant.getPledgeFee())){tenant.setPledgeFee(String.valueOf(DoubleUtil.round(Double.parseDouble(tenant.getPledgeFee()),2)));}
        if(StringUtils.isNotBlank(tenant.getDecorationPledgeFee())){tenant.setDecorationPledgeFee(String.valueOf(DoubleUtil.round(Double.parseDouble(tenant.getDecorationPledgeFee()),2)));}
        if(StringUtils.isNotBlank(tenant.getScavengingFee())){tenant.setScavengingFee(String.valueOf(DoubleUtil.round(Double.parseDouble(tenant.getScavengingFee()),2)));}
        if(StringUtils.isNotBlank(tenant.getUtilityFee())){tenant.setUtilityFee(String.valueOf(DoubleUtil.round(Double.parseDouble(tenant.getUtilityFee()),2)));}
        return tenant;
    }

    @Override
    public SysTenantHigherExcel exchangeThree(SysTenantHigherExcel tenant) {
        if(StringUtils.isNotBlank(tenant.getPledgeFee())){tenant.setPledgeFee(String.valueOf(DoubleUtil.round(Double.parseDouble(tenant.getPledgeFee()),2)));}
        return tenant;
    }
}
